package com.github.shiqiyue.learn.guava;

import com.google.common.collect.Ordering;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/***
 * Ordering 测试
 * @author wenyao.wu
 * @date 2018/7/2
 */
public class OrderingTest {

    private static final Logger logger = LoggerFactory.getLogger(OrderingTest.class);

    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<Integer>();
        numbers.add(new Integer(5));
        numbers.add(new Integer(2));
        numbers.add(new Integer(15));
        numbers.add(new Integer(51));
        numbers.add(new Integer(53));
        numbers.add(new Integer(35));
        numbers.add(new Integer(45));
        numbers.add(new Integer(32));
        numbers.add(new Integer(43));
        numbers.add(new Integer(16));
        //使用原来的对象自己实现的compare
        Ordering ordering = Ordering.natural();
        logger.info("Input List: {}", numbers);
        Collections.sort(numbers, ordering);
        logger.info("Sorted List: {}", numbers);
        //判断列表是否有序
        logger.info("List is sorted: {}", ordering.isOrdered(numbers));
        logger.info("Minimum: {}", ordering.min(numbers));
        logger.info("Maximum: {}", ordering.max(numbers));
        Collections.sort(numbers, ordering.reverse());
        logger.info("Reverse: {}", numbers);
        //添加一个新的元素，null
        numbers.add(null);
        logger.info("Null added to Sorted List: {}", numbers);

        Collections.sort(numbers, ordering.nullsFirst());
        logger.info("Null first Sorted List: {}", numbers);
        Collections.sort(numbers, ordering.nullsLast());
        logger.info("Null last Sorted List: {}", numbers);
        //普通排序时候，列表元素不能为空
//        Collections.sort(numbers, ordering);
//        logger.info("natural Sorted List: {}", numbers);
        logger.info("======================");

        List<String> names = new ArrayList<String>();
        names.add("Ram");
        names.add("Shyam");
        names.add("Mohan");
        names.add("Sohan");
        names.add("Ramesh");
        names.add("Suresh");
        names.add("Naresh");
        names.add("Mahesh");
        names.add(null);
        names.add("Vikas");
        names.add("Deepak");

        logger.info("Another List: {}", names);

        Collections.sort(names, ordering.nullsFirst().reverse());
        logger.info("Null first then reverse sorted list: {}", names);
    }
}
